package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    private final SetmealMapper setmealMapper;
    private final SetmealDishMapper setmealDishMapper;
    private final CategoryMapper categoryMapper;

    public SetmealServiceImpl(SetmealMapper setmealMapper, SetmealDishMapper setmealDishMapper, CategoryMapper categoryMapper) {
        this.setmealMapper = setmealMapper;
        this.setmealDishMapper = setmealDishMapper;
        this.categoryMapper = categoryMapper;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 新增套餐信息
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();

        // 新增套餐菜品关联信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 查询套餐列表
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryAll(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        // 查询套餐列表
        Page<SetmealVO> page = setmealMapper.queryAll(setmealPageQueryDTO);

        long total = page.getTotal();
        List<SetmealVO> setmealVOS = page.getResult();

        setmealVOS.forEach(setmealVO -> {
            // 查询套餐对应的菜品信息
           List<SetmealDish> setmealDishes = setmealDishMapper.queryBySetmealId(setmealVO.getId());
           setmealVO.setSetmealDishes(setmealDishes);

           // 查询套餐对应的分类名称
           Category category = categoryMapper.getById(setmealVO.getCategoryId());
           setmealVO.setCategoryName(category.getName());
        });

        return new PageResult(total, setmealVOS);
    }

    /**
     * 启售或停售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal().builder()
                .status(status)
                .id(id)
                .build();

        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐详情
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        // 查询套餐信息
        Setmeal setmeal = setmealMapper.getById(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        // 查询套餐对应的菜品信息
        setmealVO.setSetmealDishes(setmealDishMapper.queryBySetmealId(id));

        return setmealVO;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<Long> ids) {
        // 批量删除套餐
        setmealMapper.deleteBatch(ids);

        // 批量删除套餐对应的菜单关联信息
        setmealDishMapper.deleteBatch(ids);
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SetmealDTO setmealDTO) {
        // 更新套餐详情
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        // 删除套餐与菜单关联信息
        List<Long> setmealIds = new ArrayList<>();
        setmealIds.add(setmeal.getId());
        setmealDishMapper.deleteBatch(setmealIds);

        // 新增套餐与菜单关联信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
